package chapter07

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/10 3:43 PM
 */
object Test08_ImmutableMap {
  def main(args: Array[String]): Unit = {
    val map1: Map[String, Int] = Map("a" -> 1, "b" -> 3, "hello" -> 4)
    println(map1)
    println(map1.getClass)

    // 2.遍历元素
    map1.foreach(println)
    map1.foreach((kv: (String, Int)) => println(kv))
    println("="*20)

    // 3.取map中的所有的key 或 value
    for (key <- map1.keys) {
      println(s"$key --> ${map1.get(key)}")
    }
    // 4.访问某一个key的value
    println(map1.get("a").getOrElse(null))
    println(map1.get("c").orNull)
    println(map1.getOrElse("c", 0))
    println(map1("a"))
  }
}
